const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    bottomhegit: '100',
    code:''
  },
  getUserInfo: function (e) {
    var that = this
    var code = that.data.code
    var publicUrl = app.globalData.publicUrl
    if (e.detail.userInfo){
      if(code != ''){
        app.globalData.userInfo = e.detail.userInfo
        that.setData({
          userInfo: e.detail.userInfo,
          hasUserInfo: true
        })
        wx.request({
          url: publicUrl + 'get_userinfo', // 仅为示例，并非真实的接口地址s
          data: {
            appid: 'wx4eec067070a57604',
            secret: 'ab3c1e2766cfc6bf02d18543baf44bba',
            code: code,
            avatarUrl: e.detail.userInfo.avatarUrl,
            nickName: e.detail.userInfo.nickName,
            encryptedData: e.detail.encryptedData,
            iv: e.detail.iv
          },
          header: {
            'Content-Type': 'application/x-www-form-urlencoded'
          },
          method: "POST",
          success(res) {
            if (res) {
              wx.setStorageSync('openid', res.data.openid)
              wx.setStorageSync('uid', res.data.uid)
              wx.reLaunch({
                url: '/pages/index/index',
              })
            }
          },
          fail: function (res) {
            wx.showToast({
              title: '请确认您的手机是否连接网络',
              icon: 'none',
              duration: 3000
            })
          },
        })
      }else{
        wx.showToast({
          title: '请确认您的手机是否连接网络',
          icon: 'none',
          duration: 3000
        })
      }
      
    }else{
      console.log('拒绝')
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    wx.login({
      success: function (r) {
        var code = r.code;//登录凭证
        
        if (code) {
          that.setData({
            code: code
          })
        } else {
          console.log('获取用户登录态失败！' + r.errMsg)
        }
      },
      fail: function () {
        console.log('登陆失败')
      }
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
})